package core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

public class Props {

	private String filename = null;
	private String SEPARATOR = null;
	HashMap<String,String> map = new HashMap<String,String>();
	
	public Props(String filename,String separator)
	{
		this.filename=filename;
		this.SEPARATOR=separator;
	}
	
	public void read() throws FileNotFoundException
	{
		Scanner scanner = new Scanner(new File(filename));
		while(scanner.hasNext())
		{
			String[] array = scanner.nextLine().split(SEPARATOR);
			map.put(array[0], array[1]);
		}
		scanner.close();
	}
	
	
	public void save() throws FileNotFoundException
	{
		PrintWriter pw = new PrintWriter(new File(filename));
		for(String k : map.keySet())
		{
			pw.println(k+SEPARATOR+map.get(k));
		}
		pw.close();
	}
	
	public void add(String key,String props)
	{
		map.put(key,props);
		try {
			save();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public String getString(String k)
	{
		return map.get(k);
	}
	
	public Integer getInt(String k)
	{	
		return Integer.parseInt(map.get(k));
	}
	
	public Double getDouble(String k)
	{	
		return Double.parseDouble(map.get(k));
	}
	
	@Override
	public String toString() {	
		String str= "";
		for(String k : map.keySet())
		{
			str+=k+SEPARATOR+map.get(k)+"\n";
		}
		return str;
	}
}
